//
//  BGDBManager.m
//  BTDatabaseAccesser
//
//  Created by XMac on 2018/4/23.
//  Copyright © 2018年 XMac. All rights reserved.
//

#import "BGDBManager.h"
#import "BGDBProcess.h"
#import "BGDBConfiguration.h"
#import <FMDB/FMDB.h>
@interface BGDBManager()
@property (strong, nonatomic) BGDBProcess *process;
@property (assign, nonatomic) NSInteger version;
@end

@implementation BGDBManager

+ (nonnull instancetype)sharedManager {
    static dispatch_once_t once;
    static id instance;
    dispatch_once(&once, ^{
        instance = [self new];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        BGDBConfiguration *config = [BGDBConfiguration new];
        self.config = config;
        BGDBProcess *process = [[BGDBProcess alloc]initWithDB:self.database];
        self.process = process;
    }
    return self;
}

- (void)setConfig:(BGDBConfiguration *)config{
    _config = config;
    self.process = [[BGDBProcess alloc]initWithDB:self.database];
}



- (FMDatabase *)database
{
    if (_database && [_database open]) {
        return _database;
    }
    FMDatabase *db = [[FMDatabase alloc] initWithPath:self.config.path];
    [db open];
    self.database = db;
    [self.config onConfigure:db];
    NSInteger version = [self queryDBVersion:db];
    if (version != self.version) {
        [db beginTransaction];
        if (version == 0) {
            [self.config onCreate:db];
        } else {
            if (self.version > version) {
                [self.config onUpgrade:db oldVersion:version newVersion:self.version];
            } else {
                [self.config onDowngrade:db oldVersion:version newVersion:self.version];
            }
        }
        [self saveDBVersion:self.version database:db];
        [db commit];
    }
    [self.config onOpen:db];
    return _database;
}

- (NSInteger)queryDBVersion:(FMDatabase *)database;
{
    return [database intForQuery:@"PRAGMA user_version;"];
}

- (void)saveDBVersion:(NSInteger)version database:(FMDatabase *)database
{
    NSString *updateVersion = [[NSString alloc] initWithFormat:@"PRAGMA user_version=%d;", (int)version];
    [database executeUpdate:updateVersion];
}

#pragma
#pragma mark BGDBInnerOperationProtocol

- (BOOL)insertObject:(id)obj {
    return [self.process insertObject:obj];
}

- (BOOL)deleteWithClass:(Class)clz condition:(NSString *)condition value:(va_list)args{
    return [self.process deleteWithClass:clz condition:condition value:args];
}

- (BOOL)updateWithItem:(id)obj {
    return [self.process updateWithItem:obj];
}

- (NSArray *)queryItemsWithClass:(Class)clz condition:(NSString *)condition value:(va_list)args{
    return [self.process queryItemsWithClass:clz condition:condition value:args];
}

- (BOOL)deleteAllWithClass:(Class)clz {
    return [self.process deleteAllWithClass:clz];
}


- (BOOL)executeUpdate:(NSString *)sql {
    return [self.process executeUpdate:sql];
}

- (FMResultSet *)executeQuery:(NSString *)sql {
    return [self.process executeQuery:sql];
}




@end
